package com.adinnet.api.child.controller;


import com.adinnet.api.child.model.Child;
import com.adinnet.api.child.model.vo.ChildRankPageReq;
import com.adinnet.api.child.model.vo.ChildRankPageResp;
import com.adinnet.api.child.model.vo.ChildDetailsResp;
import com.adinnet.api.child.model.vo.ChildResp;
import com.adinnet.api.child.service.ChildService;
import com.adinnet.api.common.model.AuthUtil;
import com.adinnet.api.user.model.User;
import com.adinnet.common.utils.StringUtil;
import com.adinnet.core.JsonResult;
import com.adinnet.core.exception.BizException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 儿童表 前端控制器
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/api/child")
public class ChildController {

    @Autowired
    private ChildService childService;

    /**
     * 登陆后-查询宝宝信息（无用）
     *
     * @return
     */
    @PostMapping("details")
    public JsonResult details(HttpServletRequest request, HttpServletResponse response) {
        try {
            User loginUser = AuthUtil.getLoginUser();
            List<ChildResp> details = childService.details(loginUser.getId());
            if (CollectionUtils.isEmpty(details)) {
                return JsonResult.ok().put("data", Lists.newArrayList());
            }
            return JsonResult.ok().put("data", details);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("查询宝宝信息失败");
        }
    }


    /**
     * 确认宝宝信息
     *
     * @return
     */
    @GetMapping("confirm")
    public JsonResult confirm(Integer id, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (null == id) {
                return JsonResult.error("儿童Id不能为空");
            }
            childService.confirm(id);
            return JsonResult.ok("确认成功!");
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("确认失败!");
        }
    }

    /**
     * 查询儿童个人信息
     *
     * @return
     */
    @GetMapping("information")
    public JsonResult information(Integer id, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (null == id) {
                return JsonResult.error("儿童Id不能为空");
            }
            ChildDetailsResp childDetailsResp = childService.information(id);
            if (null == childDetailsResp) {
                return JsonResult.ok().put("data", new JSONObject());
            }
            return JsonResult.ok().put("data", childDetailsResp);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("查询儿童个人信息失败!");
        }
    }

    /**
     * 编辑儿童信息
     *
     * @return
     */
    @PostMapping("edit")
    public JsonResult edit(@RequestBody Child child) {
        try{
            if (child.getId()==null) {
                return JsonResult.error("儿童Id不能为空");
            }
            if(StringUtil.isBlank(child.getHeadUrl()) && StringUtil.isBlank(child.getBirthday()) && StringUtil.isBlank(child.getNickName())){
                return JsonResult.error("无效编辑,头像,姓名,生日不能同时为空");
            }
            childService.edit(child);
            return JsonResult.ok();
        }catch (Exception e){
            return JsonResult.error("操作失败！");
        }
    }

    /**
     * 查询儿童排名
     *
     * @return
     */
    @PostMapping("getChildRankList")
    public JsonResult getChildRankList(@Valid @RequestBody ChildRankPageReq req) {
        try{
            ChildRankPageResp resp=childService.getChildRankList(req);
            return JsonResult.ok().put("data",resp);
        }catch (Exception e){
            return JsonResult.error("查询失败！");
        }
    }

    /**
     * 清除待分享豆豆   2021-01-18新增需求
     *
     * @return
     */
    @GetMapping("empty/sharePeaseNum")
    public JsonResult clearSharePeaseNum(Integer childId) {
        try {
            if (null == childId) {
                return JsonResult.error("儿童Id不能为空");
            }
            childService.clearSharePeaseNum(childId);
            return JsonResult.ok("操作成功!");
        } catch (Exception e) {
            return JsonResult.error("清除失败！");
        }
    }
}
